package vnc;
//import java.awt.Color;
//import java.awt.GridLayout;
import java.awt.Font;
import java.awt.Image;

//import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;

import jpcap.*;

public class NicSelectPanel extends JPanel{

	////////visual///////////////////
	int width, height;
	JLabel descriptionA, descriptionB;
	JComboBox nicList;
	JButton next;
	/////////////////////////////////
	//static JpcapCaptor jpcap = null;
	NetworkInterface[] devices;
	/////////////////////////////////
	
	private ImageIcon getImageIcon(String path){
		return new ImageIcon(this.getClass().getResource(path));
	}
	
	private ImageIcon scaleImageIcon(ImageIcon icon, int width, int height){
		
		Image img = icon.getImage();
		//BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		//Graphics g = bi.createGraphics();
		//g.drawImage(img, 0, 0, width, height, null, null);
		//icon = new ImageIcon(bi);
		
		Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
		
		icon = new ImageIcon(newimg);
		
		return icon;
	}
	
	private void removeButtonBorder(JButton jb){
		jb.setBorderPainted(false);
		jb.setContentAreaFilled(false);
		jb.setDefaultCapable(false);
		jb.setFocusPainted(false);
	}
	
	public NetworkInterface getSelectedNicDevice(){
		return devices[nicList.getSelectedIndex()];
	}
	
	public int getSelectedNicIndex(){
		return nicList.getSelectedIndex();
	}
	
	public void init(int width, int height){
		this.width = width;
		this.height = height;	
		setLocation();
	}
	
	public NicSelectPanel(){
		
		devices = JpcapCaptor.getDeviceList();
		
		if(devices==null){
			JOptionPane.showMessageDialog(null,"No device found.");
			
		}else{
			String[] names=new String[devices.length];
			for(int i=0;i<names.length;i++)
				names[i]=(devices[i].description==null?devices[i].name:devices[i].description);
			nicList = new JComboBox(names);
			
		}
		
		nicList.setSelectedIndex(0);
		
		descriptionA = new JLabel("Choose NIC device, then click Capture.");
		descriptionB = new JLabel("<html>" +
				"<h3>Description</h3>" +
				"<br>" +
				"&nbsp&nbsp�� Visual Network Analyzer is a graphical Java application to display packets captured by Jpcap" +
				"<br><br><br><br>" +
				"Features" +
				"<br>" +
				"&nbsp&nbsp�� Real time capturing" +
				"<br><br>" +
				"&nbsp&nbsp�� Support Ethernet, ARP, IPv4/v6, TCP, UDP, ICMP, HTTP, FTP, SMTP, SSH, POP3, Telnet" +
				"</html>");
		//<FONT style=\"BACKGROUND-COLOR: yellow\">Feat</FONT>
		//next = new JButton(scaleImageIcon(getImageIcon("/images/next.png"), 78, 72));
		
		next = new JButton("Capture", scaleImageIcon(getImageIcon("/images/play.png"), 50, 50));
		next.setPressedIcon(scaleImageIcon(getImageIcon("/images/play_pressed.png"), 50, 50));
		next.setRolloverIcon(scaleImageIcon(getImageIcon("/images/play_rollover.png"), 50, 50));
		next.setFont(new Font("Capture", Font.ITALIC, 13));
		next.setVerticalTextPosition(SwingConstants.BOTTOM);
        next.setHorizontalTextPosition(SwingConstants.CENTER);
		removeButtonBorder(next);
		
		this.add(descriptionA);
		this.add(nicList);
		this.add(descriptionB);
		this.add(next);
		
		setLocation();
		
	}
	
	private void setLocation(){
		
		this.setLayout(null);
		
		descriptionA.setBounds(20, 30, 400, 20);
		nicList.setBounds(20, 80, 250, 20);
		descriptionB.setBounds(20, 240, 700, 250);		
		next.setBounds(this.width - 100, this.height - 120, 80, 90);
	}
}
